﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;


namespace Project_2._2.Controllers
{
    public class TinTucController : Controller
    {
        //
        // GET: /TinTuc/

        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        public ActionResult Default(int? page, string rd, string Record)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    if (rd != null)
                    {
                        int record = int.Parse(rd);
                        var tt = (from p in db.TinTucs where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                        ViewBag.record = rd;
                        return View(tt);
                    }
                    else
                    {

                        var tt = (from p in db.TinTucs where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, 3);
                        return View(tt);
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            int record = 0;
            if (string.IsNullOrEmpty(col["KeyWord"]))
            {
                record = int.Parse(col["Record"]);
                var tt = (from p in db.TinTucs where p.isDelete == false select p).ToList().ToPagedList(page ?? 1, record);
                ViewBag.record = col["Record"];
                return View(tt);
            }
            else
            {
                string SearchBy = col["SearchBy"].ToString();
                string key = col["KeyWord"].ToString();

                switch (SearchBy)
                {
                    case "TieuDe":
                        var tt = (from p in db.TinTucs where p.isDelete == false & p.TieuDe.Contains(key) select p).ToList().ToPagedList(page ?? 1, 1);
                        ViewBag.record = "1";
                        return View(tt); break;
                }
                return View();
            }
        }

        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput (false)]
        public ActionResult Create(TinTuc_ViewModels tt)
        {
            if (ModelState.IsValid)
            {
                TinTuc tintuc = new TinTuc();
                tintuc.TieuDe = tt.TieuDe;
                tintuc.NoiDung =tt.NoiDung;
                tintuc.NgayDang = tt.NgayDang;
                tintuc.isDelete = false;
                db.TinTucs.Add(tintuc);
                db.SaveChanges();
                ModelState.Clear();
                tt = new TinTuc_ViewModels();
                ModelState.AddModelError("", "Đã thêm thành công...!");
            }
            return View(tt);
        }
        public ActionResult Update(int id)
        {
            var tt = (from p in db.TinTucs where p.TinTucId == id select p).First();
            return View(tt);
        }
        [HttpPost]
        public ActionResult Update(int id, FormCollection col)
        {
            var tt = (from p in db.TinTucs where p.TinTucId == id select p).First();
            tt.TieuDe = col["TieuDe"];
            tt.NoiDung = col["NoiDung"];
            tt.NgayDang = DateTime.Parse(col["NgayDang"]);
            UpdateModel(tt);
            db.SaveChanges();
            ModelState.AddModelError("", "Cập nhật thành công.");
            return RedirectToAction("Default");
        }
    }
}
